package swd.work.conf_form_fields;

import java.util.*;

import com.pub.database.*;
import swd.base.*;

public class Business extends SwdBase {
    QueryBean gqbnDAO = null;
    public Business(QueryBean pqbnDAO) {
        super(null,null,pqbnDAO);
        gqbnDAO = pqbnDAO;

    }

    String deleteForm(String pstrPrgNo, String pstrFrmNo) throws Exception {
        ArrayList<String> laltSQL = new ArrayList<String>();

        try {
            laltSQL.add("delete from wkf_frm0 where prg_no='" + pstrPrgNo +
                        "' and frm_no='" + pstrFrmNo + "'");
            laltSQL.add("delete from wkf_frm1 where prg_no='" + pstrPrgNo +
                        "' and frm_no='" + pstrFrmNo + "'");

            gqbnDAO.executeSQL(laltSQL);

            return "delete_success";
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            if (laltSQL != null) {
                laltSQL.clear();
                laltSQL = null;
            }
        }
    }

    Hashtable<String,Object> readQueryData(String pstrPrgNo) throws Exception {
        return gqbnDAO.readSingleRowData(
                "select dbf_no,frm_name,prg_no,frm_no from wkf_frm0 where prg_no='" +
                pstrPrgNo + "'");
    }

    boolean setQueryData(View view, Hashtable<String,Object> phtQueryData) throws Exception {
        if (phtQueryData == null) {
            return false;
        }

        java.util.Enumeration<String> enuKeys = phtQueryData.keys();
        StringBuffer lstbMethodName = new StringBuffer();
        Object[] args = new Object[1];
        String lstrKeyName = null;

        while (enuKeys.hasMoreElements()) {
            lstrKeyName = enuKeys.nextElement().toString();
            lstbMethodName.append("set");
            lstbMethodName.append(lstrKeyName.toLowerCase()); //getter and setter method name 都是小寫的
            args[0] = phtQueryData.get(lstrKeyName); //要寫入view.setXXX的內容值
            view.getClass().getMethod(lstbMethodName.toString(),
                                      new Class[] {String.class}).invoke(view,
                    args); //透過Reflection將值寫入view.setXXX
            lstbMethodName.delete(0, lstbMethodName.length());
        }

        return true;
    }

    String exeUpdMasterForm(View view) throws Exception {
        String lstrSQL = new String();
        Vector<Object> lvtrParam = new Vector<Object>();
        try {
            lvtrParam.add(view.getdbf_no() == null ? null :
                          String.valueOf(view.getdbf_no()));
            lvtrParam.add(view.getfrm_name() == null ? null :
                          String.valueOf(view.getfrm_name()));
            lstrSQL = "update wkf_frm0 set dbf_no=?,frm_name=? where frm_no='" +
                      view.getfrm_no() + "'" + " and prg_no='" + view.getprg_no() +
                      "'";
            if (!gqbnDAO.executeSQL(lstrSQL, lvtrParam)) {
                return "wkf_frm0";
            }
            return "";
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.toString());
        } finally {
            lvtrParam.clear();
        }
    }

    String exeInsMasterForm(View view) throws Exception {
        String lstrSQL = new String();
        Vector<Object> lvtrParam = new Vector<Object>();
        try {
            lvtrParam.add(view.getprg_no() == null ? null :
                          String.valueOf(view.getprg_no()));
            lvtrParam.add(view.getfrm_no() == null ? null :
                          String.valueOf(view.getfrm_no()));
            lvtrParam.add(view.getdbf_no() == null ? null :
                          String.valueOf(view.getdbf_no()));
            lvtrParam.add(view.getfrm_name() == null ? null :
                          String.valueOf(view.getfrm_name()));
            lstrSQL =
                    "insert into wkf_frm0(prg_no,frm_no,dbf_no,frm_name) values (?,?,?,?)";
            if (!gqbnDAO.executeSQL(lstrSQL, lvtrParam)) {
                return "wkf_frm0";
            }
            return "";
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.toString());
        } finally {
            lvtrParam.clear();
        }
    }

    void setButtonShow(View view, String pstrPrgAction, String pstrPrgStatus) {
        view.setBtnApproveShow("none");
        view.setBtnCancelShow("none");
        view.setBtnDraftShow("none");
        view.setBtnModifyShow("none");
        view.setBtnRejectShow("none");
        view.setBtnTraceShow("none");
        view.setBtnUpdateShow("none");
        view.setBtnSaveShow("none");
        view.setBtnAddShow("none");
        view.setBtnNextFormShow("none");
        view.setBtnPrivousFormShow("none");
        view.setBtnDeleteShow("none");

        if(pstrPrgStatus!=null && pstrPrgStatus.equals("query")){
            view.setBtnNextFormShow("");
            view.setBtnPrivousFormShow("");
            view.setBtnSaveShow("");
            view.setBtnDeleteShow("");
        }
    }

    /**
     * nextFormAction
     *
     * @param view View
     * @param pstrUserNO String
     * @return Object
     */
    public String actionForNextform(View view) {
        view.setfrm_no(String.valueOf(Integer.parseInt(view.getfrm_no()) +
                                      1));
        return "";
    }

    /**
     * privousFormAction
     *
     * @param view View
     * @param pstrUserNO String
     * @return String
     */
    public String actionForPrivousform(View view) {
        view.setfrm_no(String.valueOf(Integer.parseInt(view.getfrm_no()) -
                                      1));
        return "";
    }

    /**
     * saveAction
     *
     * @param view View
     * @param pstrUserNO String
     */
    public String actionForSave(View view) throws Exception {
        if (view.getPrgStatus().equals("addMaster")) {
            exeInsMasterForm(view);
        } else {
            exeUpdMasterForm(view); /*
            if (view.getRowSn() == null || view.getRowSn().equals("")) {
            view.setOnloadScript("alert('沒有修改!');");
                                    } else {
                                        view.setOnloadScript("alert('" +
                         business.batchUpdateData(view) +
                         "');");
                                    }*/
           view.setPrgStatus("save");
            view.setprg_no(view.getprg_no());
        }
        return "";
    }

    /**
     * initAction
     *
     * @param view View
     * @param pstrUserNO String
     * @return String
     */
    public String actionForInit(View view) {
        //view.setOnloadScript("getDataViewHtml(document.WorkForm.prg_no,document.WorkForm.frm_no);");
        return "";
    }

    /**
     * deleteAction
     *
     * @param view View
     * @param pstrUserNO String
     * @return String
     */
    public String actionForDel(View view) throws Exception {
        view.setOnloadScript("alert('" +
                             view.getMessage(deleteForm(view.getprg_no(),
                view.getfrm_no())) + "');");
        return "";
    }

    /**
     * endAction
     *
     * @param view View
     * @param pstrUserNO String
     * @return String
     */
    public String actionForEnd(View view) throws Exception {
        if(view.getprg_no()==null || view.getprg_no().equals("")){
            return "";
        }

        if (setQueryData(view, readQueryData(view.getprg_no()))) {
            view.setPrgStatus("query");
            view.setDataViewHtml(getDataViewHtml(view.getDateView(),view.getDataViewForm(),view));
            //view.setOnloadScript("getDataViewHtml(document.WorkForm.prg_no,document.WorkForm.frm_no);");
        } else {
            view.setPrgStatus("addMaster");

            view.setfrm_no("0001"); //都沒有表單的預設值
        }
        return "";
    }

    public String actionForQuery(View view) throws Exception {
        return "";
    }

    public String actionForBatchUpdate(View view)throws Exception {
        String[] lstrRowSn=view.getChangeRowNum().split(",");

        try{
            for(int i=0;i<lstrRowSn.length;i++){
                //先判斷該筆是否非新增的且要刪除!!(因有可能前端按了新增或進行修改後又決定不要這筆了)
                if(!view.getDataview_row_status()[Integer.parseInt(lstrRowSn[i])].equals("I") && (view.getWillDel()!=null && view.getWillDel()[i]!=null && !view.getWillDel()[i].equals(""))){
                    if (!exeDelSlaveData(view, Integer.parseInt(lstrRowSn[i]))) {
                        throw new Exception("delete error");
                    }
                }
                else{
                    if (view.getDataview_row_status()[(Integer.parseInt(lstrRowSn[i]))].equals("I")) { //新增
                        if (!exeInsSlaveData(view, Integer.parseInt(lstrRowSn[i]))) {
                            throw new Exception("insert error");
                        }
                    } else {
                        if (!exeUpdSlaveData(view, Integer.parseInt(lstrRowSn[i]))) {
                            throw new Exception("update error");
                        }
                    }
                }

            }
            return "儲存成功!";
        }
        catch(Exception e){
            e.printStackTrace();
            return "儲存失敗!!";
       }
    }

    /**
     * exeDelSlaveData
     *
     * @param view View
     * @param pintSn int
     * @return boolean
     */
    private boolean exeDelSlaveData(View view, int pintSn) throws Exception{
        String lstrSQL=new String();
        try{
            lstrSQL="delete from wkf_frm1 where prg_no='" + view.getprg_no() + "' and frm_no='" + view.getfrm_no() + "' and seqno='" + view.getSeqno()[pintSn] + "'";

            return gqbnDAO.executeSQL(lstrSQL);
        }
        catch(Exception e){
            throw new Exception(e);
        }
    }

    /**
     * exeUpdSlaveData
     *
     * @param view View
     * @param i int
     * @return boolean
     */
    private boolean exeUpdSlaveData(View view, int pintSn) throws Exception{
        String lstrSQL=new String();
        Vector<Object> lvtrParam = new Vector<Object>();
        try{
            lvtrParam.add(view.getRw_mode()[pintSn]==null ? null :String.valueOf(view.getRw_mode()[pintSn]));
            lvtrParam.add(view.getLink_fld()[pintSn]==null ? null :String.valueOf(view.getLink_fld()[pintSn]));
            lvtrParam.add(view.getFld_kind()[pintSn]==null ? null :String.valueOf(view.getFld_kind()[pintSn]));
            lvtrParam.add(view.getFld_len()[pintSn]==null ? null :String.valueOf(view.getFld_len()[pintSn]));
            lvtrParam.add(view.getFld_max_len()[pintSn]==null ? null :String.valueOf(view.getFld_max_len()[pintSn]));
            //lvtrParam.add(view.getNot_null_yn()[pintSn]==null ? null :String.valueOf(view.getNot_null_yn()[pintSn]));
            lvtrParam.add(view.getNot_null_cndt()[pintSn]==null ? null :String.valueOf(view.getNot_null_cndt()[pintSn].toUpperCase()));
            lvtrParam.add(view.getFld_no()[pintSn]==null ? null :String.valueOf(view.getFld_no()[pintSn]));
            lvtrParam.add(view.getLink_kind()[pintSn]==null ? null :String.valueOf(view.getLink_kind()[pintSn]));
            lvtrParam.add(view.getDd_no()[pintSn]==null ? null :String.valueOf(view.getDd_no()[pintSn]));
            lvtrParam.add(view.getDd_data()[pintSn]==null ? null :String.valueOf(view.getDd_data()[pintSn]));
            lvtrParam.add(view.getLog_yn()[pintSn]==null ? null :String.valueOf(view.getLog_yn()[pintSn]));
            //lvtrParam.add(view.getReadonly_yn()[pintSn]==null ? null :String.valueOf(view.getReadonly_yn()[pintSn]));
            lvtrParam.add(view.getProtect_cndt()[pintSn]==null ? null :String.valueOf(view.getProtect_cndt()[pintSn]));
            lvtrParam.add(view.getFld_title_c()[pintSn]==null ? null :String.valueOf(view.getFld_title_c()[pintSn]));
            lvtrParam.add(view.getFld_title_e()[pintSn]==null ? null :String.valueOf(view.getFld_title_e()[pintSn].toUpperCase()));
            lvtrParam.add(view.getInit_value()[pintSn]==null ? null :String.valueOf(view.getInit_value()[pintSn]));
            //lvtrParam.add(view.getFld_hidden_yn()[pintSn]==null ? null :String.valueOf(view.getFld_hidden_yn()[pintSn]));
            lvtrParam.add(view.getFld_hidden_cndt()[pintSn]==null ? null :String.valueOf(view.getFld_hidden_cndt()[pintSn]));
            lstrSQL="update wkf_frm1 " +
                    "   set RW_MODE=?,LINK_FLD=?,FLD_KIND=?,FLD_LEN=?,FLD_MAX_LEN=?," +
                    "       NOT_NULL_CNDT=?,FLD_NO=?,LINK_KIND=?,DD_NO=?,DD_DATA=?, " +
                    "       LOG_YN=?,PROTECT_CNDT=?,FLD_TITLE_C=?,FLD_TITLE_E=?," +
                    "       INIT_VALUE=?,FLD_HIDDEN_CNDT=? " +
                    " where prg_no='" + view.getprg_no() + "'" +
                    "   and frm_no='" + view.getfrm_no() + "'" +
                    "   and seqno='" + view.getSeqno()[pintSn] + "'";

           return gqbnDAO.executeSQL(lstrSQL,lvtrParam);
        }
        catch(Exception e){
           throw new Exception(e);
        }
        finally{
           lvtrParam.clear();
           lvtrParam=null;
      }
    }

    /**
     * exeInsSlaveData
     *
     * @param view View
     * @param i int
     * @return boolean
     */
    private boolean exeInsSlaveData(View view, int pintSn) throws Exception{
        String lstrSQL=new String();
        Vector<Object> lvtrParam = new Vector<Object>();

        try{
            lstrSQL="insert into wkf_frm1(prg_no,frm_no,seqno,fld_title_c,fld_title_e,fld_no,rw_mode,fld_kind,fld_len,fld_max_len) values";
            lstrSQL=lstrSQL + DBFunction.addQuemark(lstrSQL);

            lvtrParam.add(view.getprg_no()==null ? null : String.valueOf(view.getprg_no()));
            lvtrParam.add(view.getfrm_no()==null ? null : String.valueOf(view.getfrm_no()));
            lvtrParam.add(view.getSeqno()[pintSn]==null ? null : String.valueOf(view.getSeqno()[pintSn]));
            lvtrParam.add(view.getFld_title_c()[pintSn]==null ? null : String.valueOf(view.getFld_title_c()[pintSn]));
            lvtrParam.add(view.getFld_title_e()[pintSn]==null ? null : String.valueOf(view.getFld_title_e()[pintSn]));
            lvtrParam.add(view.getFld_no()[pintSn]==null ? null : String.valueOf(view.getFld_no()[pintSn]));
            lvtrParam.add(view.getRw_mode()[pintSn]==null ? null : String.valueOf(view.getRw_mode()[pintSn]));
            lvtrParam.add(view.getFld_kind()[pintSn]==null ? null : String.valueOf(view.getFld_kind()[pintSn]));
            lvtrParam.add(view.getFld_len()[pintSn]==null ? null : String.valueOf(view.getFld_len()[pintSn]));
            lvtrParam.add(view.getFld_max_len()[pintSn]==null ? null : String.valueOf(view.getFld_max_len()[pintSn]));

            return gqbnDAO.executeSQL(lstrSQL,lvtrParam);
        }
        catch(Exception e){
            throw new Exception(e);
        }
        finally{
            lvtrParam.clear();
            lvtrParam=null;
        }
    }

    /**
     * setReadonlyFields
     *
     * @param view View
     * @param pstrPrgAction String
     * @param pstrPrgStatus String
     * @param pstrUserNO String
     * @return String
     */
    String setReadonlyFields(View view, String pstrPrgAction,
                             String pstrPrgStatus) {
        return "";
    }

    /**
     * setShowFields
     *
     * @param view View
     * @param pstrPrgAction String
     * @param pstrPrgStatus String
     * @param pstrUserNO String
     * @return String
     */
    String setShowFields(View view, String pstrPrgAction, String pstrPrgStatus) {
        return "";
    }

    /**
     * getData
     *
     * @param view View
     * @param pstrUserNO String
     * @return String
     */
    String getData(View view) throws Exception {
        if (view.getData_kind() != null &&
            view.getData_kind().equals("getDataViewHtml")) {
            return getDataViewHtml(view.getDateView(), view.getDataViewForm(),
                                   view);
        }

        if(view.getData_kind()!=null &&
           view.getData_kind().equals("rw_mode_alert")){
            return getRwModeChangeAlert(view.getRw_mode()[0],view.getChangeRowNum());
        }

        return "";
    }

    /**
     * getRwModeChangeAlert
     *
     * @param pstrRwMode String
     * @return String
     */
    private String getRwModeChangeAlert(String pstrRwMode,String pstrChangeRowNum) throws Exception{
        StringBuffer lstbRetValue=new StringBuffer();

        if(pstrRwMode.equals("H")){//hidden
            lstbRetValue.append("document.DataViewForm.link_fld[" + pstrChangeRowNum + "].value='';");
            lstbRetValue.append("document.DataViewForm.link_fld[" + pstrChangeRowNum + "].readOnly=true;");
            lstbRetValue.append("document.DataViewForm.link_fld[" + pstrChangeRowNum + "].className='input_noborder';");
        }
        else if(pstrRwMode.equals("")){

        }
        else if(pstrRwMode.equals("")){

        }
        else if(pstrRwMode.equals("")){

        }
        else if(pstrRwMode.equals("")){

        }
        else if(pstrRwMode.equals("")){

        }
        else if(pstrRwMode.equals("")){

        }
        else if(pstrRwMode.equals("")){

        }
        else if(pstrRwMode.equals("")){

        }
        else if(pstrRwMode.equals("")){

        }
        else if(pstrRwMode.equals("")){

        }
        else if(pstrRwMode.equals("")){

        }
        return lstbRetValue.toString();
    }

    String getDataViewHtml(DataView dataView, DataViewForm DataViewForm,
                           View view) throws Exception {

        DataViewForm.setTablesList(" wkf_frm1 ");

        if (view.getfrm_no() == null || view.getfrm_no().trim().equals("")) {
            view.setfrm_no(gqbnDAO.readSingleValueData(
                    "select min(frm_no) from wkf_frm1 where prg_no='" +
                    view.getprg_no() + "'"));
        }

        DataViewForm.setSQLCondition(getDataViewWhere(view));

        DataViewForm.setOrderByFields("seqno");
        DataViewForm.setOrderBySeq("asc");
        DataViewForm.setGroupBy(null);
        DataViewForm.setHaving(null);

        DataViewForm.setColRelationUseArrayList(getDataViewColRelation(view.
                getDateView(), view)); //將欄位相關設定殖入ActionForm

        DataViewForm.setSubmitMethod("POST");
        DataViewForm.setPageRowCount(0);
        DataViewForm.setJumpPageButtonShow(true); //是否要秀出上下頁按鈕
        DataViewForm.setPageBtnOnTop(true); //將button and rows info 秀在頂端
        DataViewForm.setPageBtnOnButtom(true); //將button and rows info 秀在底端
        DataViewForm.setFilterShow(false); //是否要秀出過濾選擇欄位
        DataViewForm.setJumpPageShow(false); //是否要秀出跳頁下拉式選單
        DataViewForm.setRowsInfoShow(false); //是否要秀出筆數資訊
        DataViewForm.setRowPageShow(false); //是否要秀出每頁筆數控制欄
        DataViewForm.setExcelShow(false); //是否要秀出轉Excel button
        DataViewForm.setOrderEnable(false); //是否要提供排序點選功能
        DataViewForm.setShowHeaderWhenNoData(true); //當ResultSet無資料是否要秀Header
        DataViewForm.setBatchUpdateButtonShow(true); //是否秀出批次更新鈕(需在jsp自行增加evt_dataview_btnBatchUpdate_onclick() js function)
        DataViewForm.setHeaderFix(false);
        DataViewForm.setAddNewShow(true);
        DataViewForm.setTableDataHeight("500px");

        DataViewForm.setTableHeaderHtml(
                "class='td_head_grid' align='center'  width='680px'");
        DataViewForm.setTableDataTrHtml("onmousedown=\"javascript:save_change_row_num('%sn%');\"");

        DataViewForm.setPageActionURI(getDataViewPageAction(view)); //設定上一頁與下一頁按鈕的form action(通常為母頁即本頁)

        return dataView.getOutputDataView(DataViewForm);
    }

    private String getRwModeList() {
        StringBuffer lstbFldKindList = new StringBuffer();

        lstbFldKindList.append("<option value=\"H\">");
        lstbFldKindList.append("HIDDEN");
        //lstbFldKindList.append(FormatFunction.getRepeatStr("　",8));
        lstbFldKindList.append("</option>,");
        lstbFldKindList.append("<option value=\"F\">FILE</option>,");
        lstbFldKindList.append("<option value=\"T\">TEXT BOX</option>,");
        lstbFldKindList.append("<option value=\"B\">BUTTON</option>,");
        lstbFldKindList.append("<option value=\"C\">CHECK BOX</option>,");
        lstbFldKindList.append("<option value=\"R\">RADIO BUTTON</option>,");
        lstbFldKindList.append("<option value=\"L\">LINK</option>,");
        lstbFldKindList.append("<option value=\"E\">RESET</option>,");
        lstbFldKindList.append("<option value=\"S\">SUBMIT</option>,");
        lstbFldKindList.append("<option value=\"A\">NOTE LINE</option>,");
        lstbFldKindList.append("<option value=\"D\">DROP-DOWN</option>,");
        lstbFldKindList.append("<option value=\"M\">TEXTAREA</option>,");
        lstbFldKindList.append("<option value=\"N\">RICHTEXT</option>,");
        lstbFldKindList.append("<option value=\"V\">SHOW</option>");

        return lstbFldKindList.toString();
    }

    private String getFldKindList() {
        StringBuffer lstbFldKindList = new StringBuffer();

        lstbFldKindList.append("<option value=\"C\">文字</option>,");
        lstbFldKindList.append("<option value=\"N\">數字</option>,");
        lstbFldKindList.append("<option value=\"D\">日期</option>,");
        lstbFldKindList.append("<option value=\"L\">LongRaw</option>");

        return lstbFldKindList.toString();
    }

    /**
     * getDataViewColRelation
     *
     * @param dataView DataView
     * @param view View
     * @return ArrayList
     */
    private ArrayList<Hashtable<String,Object>> getDataViewColRelation(DataView dataView, View view) {
        ArrayList<Hashtable<String,Object>> laltColRelation = new ArrayList<Hashtable<String,Object>>();
        Hashtable<String,Object> lhtColRelationDetail = new Hashtable<String,Object>();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "'Del' as del");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'Del'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "rowSpan='3' width='3%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                                 "<input type='checkbox' name='willDel' value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "'%sn%'");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML, "<input type='checkbox' name='willDel' value='' onclick='if(this.checked){this.value=(DataViewTableBody.rows.length/3)-1}'/>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "SEQNO");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'欄序'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "rowSpan='3' width='3%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML, "<input type='text' size='2' name='seqno' readonly class='input_noborder' value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML,
                "<input type='text' size='2' name='seqno' class='input' value='Ins'/>"); //使用全形空白
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "SEQNO");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "RW_MODE");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'編輯方式'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "SELECT");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "WIDTH='10%' align='middle'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                                 "<SELECT class='input' NAME=\"rw_mode\" onchange=\"javascript:evt_rw_mode_onchange(this,'%sn%');\">");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, this.getRwModeList());
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML, "<SELECT class='input' NAME='rw_mode'><option value='H'>HIDDEN</option><option value='F'>FILE</option><option value='T'>TEXT BOX</option><option value='B'>BUTTON</option><option value='C'>CHECK BOX</option><option value='R'>RADIO BUTTON</option><option value='L'>LINK</option><option value='E'>RESET</option><option value='S'>SUBMIT</option><option value='A'>NOTE LINE</option><option value='D'>DROP-DOWN</option><option value='M'>TEXTAREA</option><option value='N'>RICHTEXT</option><option value='V'>SHOW</option></select></select>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "LINK_FLD");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'串主檔欄'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "WIDTH='10%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                "<input type='text' size='20' maxlength='40' name='link_fld' value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "LINK_FLD");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML,
                "<input type='text' size='20' maxlength='40' name='link_fld' value='LINK_FLD'/>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "FLD_KIND");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'資料型態'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "SELECT");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "WIDTH='10%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                                 "<SELECT class='input' NAME='fld_kind' onchange=\"evt_fld_kind_onchange(this);\" onfocus=\"evt_fld_kind_onfocus(this)\">");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, this.getFldKindList());
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML, "<SELECT class='input' NAME='fld_kind'><option value='C'>文字</option><option value='N'>數字</option><option value='D'>日期</option><option value='L'>LongRaw</option></select>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "FLD_LEN");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'長度'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "WIDTH='10%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                "<input type='text' size='20' maxlength='40' name='fld_len' value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "FLD_LEN");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML,
                "<input type='text' size='20' maxlength='40' name='fld_len' value='0'/>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "FLD_MAX_LEN");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'限'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "WIDTH='10%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                "<input type='text' size='20' maxlength='40' name='fld_max_len' value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "FLD_MAX_LEN");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML,
                "<input type='text' size='20' maxlength='40' name='fld_max_len' value='0'/>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        /*lhtColRelationDetail.put(dataView.STR_FIELD_NAME,"NOT_NULL_YN");
                lhtColRelationDetail.put(dataView.STR_FIELD_TITLE,"'欄位必填'");
                lhtColRelationDetail.put(dataView.STR_FIELD_KIND,"INPUT");
                lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,"WIDTH='10%' align='center'");
                lhtColRelationDetail.put(dataView.STR_FIELD_HTML,"<input type='text' size='20' maxlength='40' name='not_null_yn' value=");
         lhtColRelationDetail.put(dataView.STR_FIELD_VALUE,"NOT_NULL_YN");
                lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML,"<input type='text' size='20' maxlength='40' name='not_null_yn' value='NOT_NULL_YN'/>");

                laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
                lhtColRelationDetail.clear();*/

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "NOT_NULL_CNDT");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'必填條件'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "WIDTH='10%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                "<input type='text' size='20' maxlength='40' name='not_null_cndt' value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "NOT_NULL_CNDT");
        lhtColRelationDetail.put(dataView.STR_FIELD_LINE_FEED, "true");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML, "<input type='text' size='20' maxlength='40' name='not_null_cndt' value='NOT_NULL_CNDT'/>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "FLD_NO");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'欄位名稱'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "WIDTH='10%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                "<input type='text' size='20' maxlength='40' name='fld_no' value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "FLD_NO");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML,
                "<input type='text' size='20' maxlength='40' name='fld_no' value='FLD_NO'/>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "LINK_KIND");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'連動動作'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "WIDTH='10%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                "<input type='text' size='20' maxlength='40' name='link_kind' value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "LINK_KIND");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML, "<input type='text' size='20' maxlength='40' name='link_kind' value='LINK_KIND'/>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "DD_NO");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'輔助設定'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "WIDTH='10%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                "<input type='text' size='20' maxlength='40' name='dd_no' value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "DD_NO");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML,
                "<input type='text' size='20' maxlength='40' name='dd_no' value='DD_NO'/>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "DD_DATA");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'參數設定'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "WIDTH='10%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                "<input type='text' size='20' maxlength='40' name='dd_data' value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "DD_DATA");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML,
                "<input type='text' size='20' maxlength='40' name='dd_data' value='DD_DATA'/>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "LOG_YN");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'Log'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "WIDTH='10%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                "<input type='text' size='20' maxlength='40' name='log_yn' value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "LOG_YN");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML,
                "<input type='text' size='20' maxlength='40' name='log_yn' value='LOG_YN'/>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        /*lhtColRelationDetail.put(dataView.STR_FIELD_NAME,"READONLY_YN");
                lhtColRelationDetail.put(dataView.STR_FIELD_TITLE,"'欄位唯讀'");
                lhtColRelationDetail.put(dataView.STR_FIELD_KIND,"INPUT");
                lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,"WIDTH='10%' align='center'");
                lhtColRelationDetail.put(dataView.STR_FIELD_HTML,"<input type='text' size='20' maxlength='40' name='readonly_yn' value=");
         lhtColRelationDetail.put(dataView.STR_FIELD_VALUE,"READONLY_YN");
                lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML,"<input type='text' size='20' maxlength='40' name='readonly_yn' value='READONLY_YN'/>");

                laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
                lhtColRelationDetail.clear();*/

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "PROTECT_CNDT");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'唯讀條件'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "WIDTH='10%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                "<input type='text' size='20' maxlength='40' name='protect_cndt' value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "PROTECT_CNDT");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML, "<input type='text' size='20' maxlength='40' name='protect_cndt' value='PROTECT_CNDT'/>");
        lhtColRelationDetail.put(dataView.STR_FIELD_LINE_FEED, "true");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "FLD_TITLE_C");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'中文名稱'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "WIDTH='10%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                "<input type='text' size='20' maxlength='40' name='fld_title_c' value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "FLD_TITLE_C");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML, "<input type='text' size='20' maxlength='40' name='fld_title_c' value='FLD_TITLE_C'/>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "FLD_TITLE_E");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'英文名稱'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "WIDTH='10%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                "<input type='text' size='20' maxlength='40' name='fld_title_e' value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "FLD_TITLE_E");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML, "<input type='text' size='20' maxlength='40' name='fld_title_e' value='FLD_TITLE_E'/>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "INIT_VALUE");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'欄位初始設定'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "colSpan='3' WIDTH='10%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                "<input type='text' size='77' maxlength='40' name='init_value' value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "INIT_VALUE");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML, "<input type='text' size='77' maxlength='40' name='init_value' value='INIT_VALUE'/>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        /*lhtColRelationDetail.put(dataView.STR_FIELD_NAME,"FLD_HIDDEN_YN");
                lhtColRelationDetail.put(dataView.STR_FIELD_TITLE,"'欄位隱藏'");
                lhtColRelationDetail.put(dataView.STR_FIELD_KIND,"INPUT");
                lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,"WIDTH='10%' align='center'");
                lhtColRelationDetail.put(dataView.STR_FIELD_HTML,"<input type='text' size='20' maxlength='40' name='fld_hidden_yn' value=");
         lhtColRelationDetail.put(dataView.STR_FIELD_VALUE,"FLD_HIDDEN_YN");
                lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML,"<input type='text' size='20' maxlength='40' name='fld_hidden_yn' value='FLD_HIDDEN_YN'/>");

                laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
                lhtColRelationDetail.clear();*/

        lhtColRelationDetail.put(dataView.STR_FIELD_NAME, "FLD_HIDDEN_CNDT");
        lhtColRelationDetail.put(dataView.STR_FIELD_TITLE, "'隱藏條件'");
        lhtColRelationDetail.put(dataView.STR_FIELD_KIND, "INPUT");
        lhtColRelationDetail.put(dataView.STR_FIELD_PROPERTY,
                                 "WIDTH='10%' align='center'");
        lhtColRelationDetail.put(dataView.STR_FIELD_HTML,
                "<input type='text' size='20' maxlength='40' name='fld_hidden_cndt' value=");
        lhtColRelationDetail.put(dataView.STR_FIELD_VALUE, "FLD_HIDDEN_CNDT");
        lhtColRelationDetail.put(dataView.STR_FIELD_APPEND_ROW_HTML, "<input type='text' size='20' maxlength='40' name='fld_hidden_cndt' value='FLD_HIDDEN_CNDT'>");

        laltColRelation.add(new Hashtable<String,Object>(lhtColRelationDetail));
        lhtColRelationDetail.clear();

        return laltColRelation;
    }

    /**
     * getDataViewPageAction
     *
     * @param view View
     * @return String
     */
    private String getDataViewPageAction(View view) {
        StringBuffer stbPageActionURI = new StringBuffer(
                "conf_form_fields.jsp?prgAction=&frm_no=");

        stbPageActionURI.append(view.getfrm_no());
        stbPageActionURI.append("&prg_no=");
        stbPageActionURI.append(view.getprg_no());

        return stbPageActionURI.toString();
    }

    /**
     * getDataViewWhere
     *
     * @param view View
     * @return Object
     */
    private String getDataViewWhere(View view) throws Exception {
        StringBuffer stbCondition = new StringBuffer();

        if (view.getprg_no() != null && !view.getprg_no().trim().equals("")) {
            stbCondition.append(" prg_no='");
            stbCondition.append(view.getprg_no());
            stbCondition.append("'");
        }

        if (view.getfrm_no() != null && !view.getfrm_no().trim().equals("")) {
            stbCondition.append(" and frm_no='");
            stbCondition.append(view.getfrm_no());
            stbCondition.append("'");
        }
        return stbCondition.toString();
    }
}
